$(function () {
	// 查询列表
	querySearch()

	// 查询枚举类
	$.get(baseURL + "purchase/storepurchaseapply/enumlist",
		function(r){
			vm.e.tax = r.purchaseApplyTaxEnum;
			vm.e.MoneyStatus = r.purchaseApplyFinanceStatusEnum;
		});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		dialogFormVisible: false,
		costFormVisible: false,
		ids: [], //存放被选择的列的id项
		statuss: [], // 存放被选择的状态项
		page:{
			currPage:1, // 当前页
			pageSize:20, // 分页数
			totalCount:0, // 总条数
			tableData:[], //数据列
		},
		q:{
			applyNo:"",
			beginCreatetime:"",
			endCreatetime:"",
			status:"",
			tax:"",
		},
		e:{ //枚举类
			purchaseApplyFinanceStatusList: [
				{value: '0',label: '待审核'},
				{value: '1',label: '待付款'},
				{value: '2',label: '已付款'}
			],
			purchaseApplyTaxList: [
				{value: '0',label: '含税'},
				{value: '1',label: '不含税'}
			],
			tax:[],
			MoneyStatus:[],
            //采购类型
            purchaseTypeList: [
                {value: 0,label: '普通耗材'},
                {value: 1,label: '纸张'}
            ],
            purchaseApplyStatusList: [
                {value: '1',label: '待审核'},
                {value: '2',label: '已审核'},
                {value: '3',label: '已入库'},
                {value: '4',label: '驳回'}
            ],
		},
		purchaseApplyForm:{
			storePurchaseApplyEntity:{
				financeStatus:null,
				tax:null
			}
		},
		//---------------------------------实际入库详细信息
        storeMaterialInAndDetailList:[],
        purchaseRow:null,
        otherAccessoryDialog:false,
        otherAccessoryList:[],
        storePurchaseApplyEntity:{},
	},
    updated: function(){
        if (this.otherAccessoryList.length >0){
            $('#view').viewer('destroy');
            $('#view').viewer();
        }
    },
	methods: {
		query: function () {
			vm.reload();
		},

		BatchAudit: function () {
			var sdata = this.ids;
			if (this.$refs.ReimbursementTable.selection.length == 0){
				this.$message.error('至少选中一条数据 !');
				return;
			}
			var ppp = this.statuss;
			for(var i = 0;i<ppp.length;i++){
				if(ppp[i]!=0){
					alert("请确保都是待审批状态");
					return
				}
			}
			var url = "purchase/storepurchaseapply/BatchAudit";
			$.ajax({
				type: "POST",
				url: baseURL + url ,
				contentType: "application/json",
				data:JSON.stringify(sdata),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},

		BulkPayment: function () {
			var sdata = this.ids;
			console.log(sdata);
			if (this.$refs.ReimbursementTable.selection.length == 0){
				this.$message.error('至少选中一条数据 !');
				return;
			}
			var ddd = this.statuss;
			for(var i = 0;i<ddd.length;i++){
				if(ddd[i]!=1){
					alert("请确保都是待付款状态");
					return
				}
			}
			var url = "purchase/storepurchaseapply/BulkPayment";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data:JSON.stringify(sdata),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},

		selectChange: function(row){
			if(row.length > 0) {
				var ids = this.formatAllId(row)
			}
			this.ids = ids

			if(row.length > 0){
				var statuss = this.sss(row)
			}
			this.statuss = statuss
		},

		formatAllId: function(row) {
			var ids = row.map(function(value){
				return value.applyId
			})
			return ids
		},

		sss: function (row) {
			var statuss = row.map(function(value){
				return value.financeStatus
			})
			return statuss
		},

		getInfo: function(goodsId){
			$.get(baseURL + "goods/goods/info/"+goodsId, function(r){
			});
		},
		reset:function(){
			vm.q.applyNo= '';
			vm.q.beginCreatetime="";
			vm.q.endCreatetime="";
			vm.purchaseApplyForm.storePurchaseApplyEntity.financeStatus=null;
			vm.purchaseApplyForm.storePurchaseApplyEntity.tax=null;
		},
		reload: function (event) {
			querySearch()
		},
		/* 订单起始 */
		checkBeginDate: function () {
			WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
					vm.q.beginCreatetime = dp.cal.getNewDateStr();
				}})
		},
		/* 订单结束 */
		checkEndDate: function () {
			WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
					vm.q.endCreatetime = dp.cal.getNewDateStr();
				}})
		},


		/*格式化 含税状态*/
		formatTax: function (row, column, cellValue, index) {
			for(var i=0; i<vm.e.tax.length; i++){
				if(cellValue == vm.e.tax[i].id){
					return vm.e.tax[i].describe
				}
			}
		},
		/*格式化 财务审核状态*/
	formatStatus: function (row, column, cellValue, index) {
		for(var i=0; i<vm.e.MoneyStatus.length; i++){
			if(cellValue == vm.e.MoneyStatus[i].id){
			return vm.e.MoneyStatus[i].describe
			}
		}
	},
        /*格式化 采购类型*/
        formatPurchase: function (row, column, cellValue, index) {
            for(var i=0; i<vm.e.purchaseTypeList.length; i++){
                if(cellValue == vm.e.purchaseTypeList[i].value){
                    return vm.e.purchaseTypeList[i].label
                }
            }
        },
        /*格式化 采购申请单状态*/
        formatPurchaseApplyStatus: function (row, column, cellValue, index) {
            for(var i= 0; i<vm.e.purchaseApplyStatusList.length; i++){
                if(cellValue == vm.e.purchaseApplyStatusList[i].value){
                    return vm.e.purchaseApplyStatusList[i].label
                }
            }
        },

		/*分页数改变*/
		handleSizeChange(val) {
			vm.page.pageSize = val;
			querySearch();
		},

		/*分页当前页改变改变*/
		handleCurrentChange(val) {
			vm.page.currPage = val
			querySearch();
		},
        auditTwo:function(){
            vm.audit(0,vm.purchaseRow);
            vm.reloadElement();
        },
		/*审核*/
		audit: function(index, row) {
            if(vm.checkStatus(row)){
            	return;
			}
			var url = "purchase/storepurchaseapply/Approval/"+row.applyId;
			this.$confirm('确认审核通过?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				$.ajax({
					type: "GET",
					url: baseURL + url,
					contentType: "application/json",
					success: function(r){
						if(r.code == 0){
							alert(r.msg);
							querySearch();

						}else{
							alert(r.msg);
						}
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消审核'
				});
			});
		},
        auditAgainCheckTwo:function(){
			vm.auditAgainCheck(0,vm.purchaseRow);
            vm.reloadElement();
		},
		/*复审*/
		auditAgainCheck: function(index,row){
            if(vm.checkStatus(row)){
                return;
            }
			var url = "purchase/storepurchaseapply/auditAgainCheck/"+row.applyId;
			this.$confirm('确认复审?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				$.ajax({
					type: "GET",
					url: baseURL + url,
					contentType: "application/json",
					success: function(r){
						if(r.code == 0){
							alert(r.msg);
							querySearch();
						}else{
							alert(r.msg);
						}
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消复审'
				});
			});
		},
        OnePayTwo:function(){
            vm.OnePay(0,vm.purchaseRow);
            vm.reloadElement();
        },
		/*付款*/
		OnePay: function (index,row) {
            if(vm.checkStatus(row)){
                return;
            }
			var url = "purchase/storepurchaseapply/OnePay/"+row.applyId;
			this.$confirm('确认付款?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				$.ajax({
					type: "GET",
					url: baseURL + url,
					contentType: "application/json",
					success: function(r){
						if(r.code == 0){
							alert(r.msg);
							querySearch();
						}else{
							alert(r.msg);
						}
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消付款'
				});
			});
			
		},
        backTwo:function(){
            vm.back(0,vm.purchaseRow);
            vm.reloadElement();
        },
		/*弃审*/
		back: function(index, row) {
            if(vm.checkStatus(row)){
                return;
            }
			var url = "purchase/storepurchaseapply/AbandonmentOfTrial/"+row.applyId;
			this.$confirm('确认弃审?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				$.ajax({
					type: "GET",
					url: baseURL + url,
					contentType: "application/json",
					success: function(r){
						if(r.code == 0){
							alert(r.msg);
							querySearch();
						}else{
							alert(r.msg);
						}
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消弃审'
				});
			});
		},
		//查看功能
        examine:function(index,row){
            if(vm.checkStatus(row)){
                return;
            }
			vm.purchaseRow = row;
			vm.showList = false;
			//拿到采购单上的数据
			vm.storePurchaseApplyEntity = row;
			//拿到该单据的所有采购信息
            var url = "storeMaterialIn/storematerialin/getStoreMaterialInByApplyNo/"+row.applyNo;
			$.ajax({
                type: "GET",
                url: baseURL + url,
                contentType: "application/json",
                success: function(r){
                    if(r.code == 0){
						vm.storeMaterialInAndDetailList = r.storeMaterialInAndDetailList;
                    }else{
                        alert(r.msg);
                    }
                }
			})
		},
        getBack:function(){
            vm.showList = true;
            vm.storeMaterialInAndDetailList = [];
		},
        view:function(otherAccessoryList){
			vm.otherAccessoryList = otherAccessoryList;
			vm.otherAccessoryDialog = true;
		},
		//重置元素
        reloadElement:function(){
            vm.storeMaterialInAndDetailList = [];
            vm.showList = true;
            vm.otherAccessoryList=[];
            vm.purchaseRow = null;
		},
		checkStatus(row){
            if(row.status != 3){
                alert("采购单已审核但未入库,请确认系统入库信息")
                return true;
            }
		}
	}
});

// 列表查询
function querySearch() {
	var queryData = {
		limit:vm.page.pageSize,
		page:vm.page.currPage,
		applyNo:vm.q.applyNo,
		beginCreatetime:vm.q.beginCreatetime,
		endCreatetime:vm.q.endCreatetime,
		status:vm.purchaseApplyForm.storePurchaseApplyEntity.financeStatus,
		tax:vm.purchaseApplyForm.storePurchaseApplyEntity.tax
	}
	$.ajax({
		type: "POST",
		url: baseURL + "purchase/storepurchaseapply/selectAllList",
		contentType: "application/json",
		data: JSON.stringify(queryData),
		success: function(r){
			vm.page.tableData = r.page.list
			vm.page.totalCount = r.page.totalCount
		}
	});
}
